class Solution {
    public int uniquePaths(int m, int n) {
        if (m == 1 || n == 1) return 1;
        int k = Math.min(m,n);
        long res = 1;
        for (int i = 1; i <= k-1; i++) {
            res *= (m+n-1-i);
            res /= i;
        }
        return (int) res;
    }

    
    public int uniquePaths1(int m, int n) {
        int[] dp = new int[n];
        dp[0] = 1;
        for (int i = 0; i < m; i++){
            for (int j = 1; j < n; j++){
                dp[j] += dp[j-1];
            }
        }
        return dp[n-1];
    }
}